package com.bolt.auth.security.common.utils;

import com.bolt.common.BoltConstants;
import com.bolt.common.utils.StrUtil;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**.
 * Created by Administrator on 2020/2/28.
 */
public class TokenUtil {

    public static String getToken (){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        String header = request.getHeader(BoltConstants.AUTH_HEADER) ;
        String token = StrUtil.isBlank(StrUtil.subAfter(header, OAuth2AccessToken.BEARER_TYPE+" ",true)) ? request.getParameter(OAuth2AccessToken.ACCESS_TOKEN) :  StrUtil.subAfter(header, OAuth2AccessToken.BEARER_TYPE +" ",true);

        token = StrUtil.isBlank(request.getHeader(BoltConstants.APP_AUTH_ACCESS_TOKEN)) ? token : request.getHeader(BoltConstants.AUTH_HEADER) ;


        return token ;

    }
}

 